<?php

namespace App\Admin\Forms;

use App\Models\TongUser;
use App\Models\TongUserOpera;
use Dcat\Admin\Admin;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;

class UpdateIdentity extends Form
{
    use LazyWidget;
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        // dump($input);

        // return $this->response()->error('Your error message.');
        DB::transaction(function () use($input) {
            $user_info = TongUser::query()->where('id',$input['id'])->first();
            $pre = $user_info['identity_id'];
        TongUser::query()->where('id',$input['id'])->update(['identity_id'=>$input['identity_id']]);
        updateUserIdentity($input['id'],$input['identity_id']);

            //增加操作记录
            $admin_id = Admin::user()->id;
            $con_usr_i = config('user.identity');
            TongUserOpera::query()->create([
                'admin_id'=>$admin_id,
                'type'=>4,
                'u_id'=>$input['id'],
                'desc'=>json_encode(['pre'=>'用户身份修改前：'.$con_usr_i[$pre],'next'=>'用户身份修改后：'.$con_usr_i[$input['identity_id']]])
            ]);

        });
        return $this
            ->response()
            ->success('修改身份成功')
            ->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->hidden('id', 'id')->required();
        //$this->select('money','充值金额')->required();
        $this->select('identity_id', '身份')->options(config('user.identity'))->required();
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [
            'id' => $this->payload['id'] ?? null,
            'identity_id' => $this->payload['identity_id'] ?? 0,
        ];
    }
}
